package com.wy2.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wy2.entity.Module;
import com.wy2.entity.RoleModule;
import com.wy2.mapper.ModuleMapper;
import com.wy2.mapper.RoleModuleMapper;
import com.wy2.service.ModuleService;
import com.wy2.util.FenyeUtil;

@Service
public class ModuleServiceImpl implements ModuleService {

	@Autowired
	private ModuleMapper mm;
	
	@Autowired
	private RoleModuleMapper rm;
	/**
	 * 获取左侧栏
	 */
	@Override
	public List<Module> getModuleByUser(String uid) {
		List<Module> list = mm.getAll(uid);
		List<Module> Module = new ArrayList<Module>();
		for(Module t : list) {
			if(t.getParentid() == 0) {
				Module.add(findChildren(t,list));
			}
		}
		
		return Module;
	}
	private Module findChildren(Module t, List<Module> list) {
		for(Module item : list) {
			if(t.getId() == item.getParentid()) {
				if(t.getChildren() == null) {
					t.setChildren(new ArrayList<>());
				}
				t.getChildren().add(findChildren(item,list));
			}
		}
		
		
		return t;
	}
	@Override
	public List<Module> findBytitle(String title) {
		// TODO Auto-generated method stub
		return mm.findBytitle(title);
	}
	@Override
	public Module getM(String id) {
		List<Map<String, Object>> map = new ArrayList<>();
		return mm.getM(id);
	}
	@Override
	public Map<String, Object> addMod(Module m) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		String name = m.getTitle();
		// 查找该角色是否存在
		Module isname = mm.getTitle(name);
		// 不等于null 证明存在
		if (isname != null) {
			// 返回信息
			map.put("msg", "该模块【" + name + "】已存在");
			return map;
		}
		Integer save = mm.addMod(m);
		if (save != null) {
			map.put("code", 0);
			map.put("msg", "保存成功");
		} else {
			map.put("msg", "保存失败");
		}
		return map;
	}
	@Override
	public Map<String, Object> updMod(Module m) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		String name = m.getTitle();
		// 查找该角色是否存在
		Module isname = mm.getTitle(name);
		// 不等于null 证明存在
		if (isname != null) {
			// 返回信息
			map.put("msg", "该模块【" + name + "】已存在");
			return map;
		}
		Integer save = mm.updMod(m);
		if (save != null) {
			map.put("code", 0);
			map.put("msg", "保存成功");
		} else {
			map.put("msg", "保存失败");
		}
		return map;
	}
	@Override
	public Object delMod(String id) {
		List<String> ids = Arrays.asList(id.split(","));
		Map<String, Object> map = new HashMap<>();
		System.out.println("---map"+map);
		List<Module> mou = mm.findByIds(id);
		System.out.println("---mou"+mou);
		List<Module> me = mm.getByRole(id);
		System.out.println("---me"+me);
		if(mou.size() == 0 && me.size() == 0) {
			Integer num = mm.delMod(ids);
			map.put("num", num!=0?0:1);
			return map;
		}
		map.put("msg", "改模块下有其他模块 或该模块下有角色设置 不能进行删除");

		return map;
	}
	@Override
	public FenyeUtil<Module> getMod(FenyeUtil<Module> fenye) {
		Integer count = mm.getCount(fenye);
		List<Module> data = mm.getData(fenye);
		if(data.size()>0) {
			fenye.setCount(count);
			fenye.setData(data);
		}else {
			fenye.setCode(1);
			fenye.setMsg("无数据");
		}
		return fenye;
	}
	/**
	 * 给角色设置模块
	 */
	@Override
	public Map<String, Object> AddandDel(String rid, List<Integer> id) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		mm.delRidByid(rid);
		System.out.println(rid);
		System.out.println(id);
		Integer addnum = 0;
		for(int i = 0 ; i<id.size();i++) {
			int setmid = id.get(i);
			addnum = mm.addRidMid(rid,setmid);
			System.out.println(addnum);
		}
		if(addnum !=0) {
			map.put("code", 0);
			map.put("msg", "设置成功");
			return map;
		}else {
			map.put("code", 1);
			map.put("msg", "设置失败");
			return map;
		}
	}

}
